package com.linkstec.config;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import org.springdoc.core.customizers.GlobalOpenApiCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;

/**
 * @Author linkage
 */
@Configuration
@EnableKnife4j // 开启 knife4j，可以不写
public class Knife4jConfig implements WebMvcConfigurer {

    /**
     *
     * 显示swagger-ui.html文档展示页，还必须注入swagger资源：
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    /**
     * 根据@Tag 上的排序，写入x-order
     *
     * @return the global open api customizer
     */
    @Bean
    public GlobalOpenApiCustomizer orderGlobalOpenApiCustomizer() {
        return openApi -> {
            if (openApi.getTags() != null) {
                openApi.getTags().forEach(tag -> {
                    Map<String, Object> map = new HashMap<>();
                    map.put("x-order", new Random(100).nextInt());
                    tag.setExtensions(map);
                });
            }
            if (openApi.getPaths() != null) {
                openApi.addExtension("x-test123", "333");
                openApi.getPaths().addExtension("x-abb", new Random(100).nextInt());
            }

        };
    }

    /**
     * api文档的详细信息函数,注意这里的注解引用的是哪个
     *
     * @return
     */
    /*
     * private OpenAPI apiInfo() { return new OpenAPI().info(new Info() // //大标题
     * .title("spring-boot-admin 后台服务API接口文档") // 版本号 .version("1.0.0") //
     * .termsOfServiceUrl("NO terms of service") // 描述 .description("后台API接口") // 作者
     * .contact(new Contact().name("凌志软件股份有限公司").url("www.linkstec.com").email(
     * "linkstec@linkstec.com")) .license(new
     * License().name("The Apache License, Version 2.0")
     * .url("http://www.apache.org/licenses/LICENSE-2.0.html"))); }
     */

}
